package aspirateur;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintWriter;
import java.util.ArrayList;

public class PageDownloader extends FileDownloader {

	private ArrayList<String> listeHtml;
	private String url;
	private String racine;
	
	public PageDownloader(String _url, ArrayList<String> a, String src, String dest) {
		super(src, dest);
		listeHtml = a;
		url = GenerateurDeNom.genererNomRelatif(_url, src);
		url = GenerateurDeNom.ajouterExtensionHtml(url);
		creerDossierRacine(src);
	}
	
	private void creerDossierRacine(String src) {
		racine = src.substring(7, src.length());
		File dossier = new File(destination+"/"+racine);
		dossier.mkdirs();
	}

	public void save() {
		if (url.contains("/")) {
			String reps = url.substring(0, url.lastIndexOf("/"));
			File dossiers = new File(destination+"/"+racine+"/"+reps+"/");
			dossiers.mkdirs();
		}
		File fichier = new File(destination+"/"+racine+"/"+url);
		PrintWriter pw;
		try {
			pw = new PrintWriter(new FileOutputStream(fichier));
			for (int i=0; i<listeHtml.size(); i++)
				pw.print(listeHtml.get(i));
			
			pw.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		
	}
	
}
